<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mall-manager:goods-unit:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mall-manager:goods-unit:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="unitList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="计量单位" align="center"  prop="name" />

      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="更新时间" align="center" prop="updateTime"/>
      <el-table-column label="创建人" align="center" prop="createBy"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-dropdown trigger="click">
            <span class="el-dropdown-link">
              更多<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown" >

              <el-dropdown-item  v-hasPermi="['mall-manager:goods-unit:edit']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"

                >修改
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item command="a"   v-hasPermi="['mall-manager:goods-unit:remove']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"

                >删除
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--  下架商品  -->
    <el-dialog :title="title" :visible.sync="open" v-if="open" width="50%" @close="open = false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="计量单位" prop="name">
          <el-input v-model="form.name" placeholder="请输入计量单位" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listGoodsUnit, addGoodsUnit, editGoodsUnit,getGoodsUnit,delGoodsUnit } from "@/api/mall/goods/unit";
  export default {
    name: "goodsUnit",
    data() {
      return {
        id: "", //要操作的id
        loading: true, // 表单加载状态
        modalVisible: false, // 添加或编辑显示
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
        form: {
          name: "",
        },
        // 表单校验
        rules: {
          name: [
            { required: true, message: "计量单位不能为空", trigger: "blur" },
            { min: 1, max: 5, message: '计量单位长度必须介于 1 和 5 之间', trigger: 'blur' }
          ],
        },
        total: 0, // 表单数据总数
        unitList: [],
        open: false,
        title: ''
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 查询商品列表 */
      getList() {
        this.loading = true;
        listGoodsUnit(this.queryParams).then(response => {
          this.unitList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增操作  */
      handleAdd(){
        this.open = true;
        this.title = '新增计量单位'
        this.reset();
      },
      /** 修改操作  */
      handleUpdate(row){
        this.reset();
        const id = row.id || this.ids
        getGoodsUnit(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改计量单位";
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
          return delGoodsUnit(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      reset(){
        this.form = {
          name: '',
        };
        this.resetForm("form");
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              editGoodsUnit(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addGoodsUnit(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
    },
  };
</script>

